package clean_handler

import (
	"file-cleaner/internal/file_cleaner/cleaner"
	v1log "github.com/ClessLi/component-base/pkg/log/v1"
	"os"
)

type CleanHandler interface {
	doHandle(info *cleaner.CleanTaskInfo) bool
}

type DeleteFileHandlerImpl struct {
}

func (d DeleteFileHandlerImpl) doHandle(info *cleaner.CleanTaskInfo) bool {
	err := os.Remove(info.SourceFileInfo().Path())
	if err != nil {
		v1log.Error(err.Error())
		return false
	}
	v1log.Infof("Finished deleting file: %v", info.SourceFileInfo().Path())
	return true
}

type LinkFileHandlerImpl struct {
}

func (l LinkFileHandlerImpl) doHandle(info *cleaner.CleanTaskInfo) bool {
	err := os.Link(info.TargetFileInfo().Path(), info.SourceFileInfo().Path())
	if err != nil {
		v1log.Error(err.Error())
		return false
	}
	v1log.Infof("Finished hard link file: %v (size: %v, hash: %v), to file: %v (size: %v, hash: %v)", info.SourceFileInfo().Path(), info.SourceFileInfo().Size(), info.SourceFileInfo().Hash(), info.TargetFileInfo().Path(), info.TargetFileInfo().Size(), info.TargetFileInfo().Hash())
	return true
}

type DryRunHandlerImpl struct {
}

func (d DryRunHandlerImpl) doHandle(info *cleaner.CleanTaskInfo) bool {
	v1log.Infof("DryRun File name: %v", info.SourceFileInfo().Path())

	return true
}

type SuccessfulRecordHandlerImpl struct {
	collector *CleanTaskInfoCollector
}

func NewSucRecordHandler(collector *CleanTaskInfoCollector) CleanHandler {
	handler := &SuccessfulRecordHandlerImpl{collector: collector}
	return CleanHandler(handler)
}

func (a *SuccessfulRecordHandlerImpl) doHandle(info *cleaner.CleanTaskInfo) bool {
	a.collector.Record(info, true)
	return true
}
